package com.sm.controller;

import java.util.List;

import com.sm.model.Collect;
import com.sm.model.Teacher;
import com.sm.model.User;
import com.sm.model.VideoCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sm.service.CollectService;
import com.sm.service.TeacherService;
import com.sm.service.UserService;
import com.sm.service.VideoCourseService;
import com.sm.util.Msg;

@Controller
@RequestMapping("/video")
public class SortVideoController {
	
	@Autowired
	private VideoCourseService videoCourseService;
	@Autowired
	private UserService userService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private CollectService collectService;
	
	
	
	/**
	 *	根据用户的id查询所有的免费的视频
	 * @param userId		用户id
	 * @return
	 */
	@RequestMapping("/free")
	@ResponseBody
	public Msg selectFreeVideo(Integer userId) {
		if(userId == null) {
			return Msg.fail().add("msg", "数据异常请重试");
		}
		User user = userService.selectUserById(userId);//根据用户id查询用户信息。
		if(user == null) {
			return Msg.fail().add("msg", "数据异常请重试");
		}
		//如果当前用户是普通用户
//		if(user.getRole() == 1 && user.getManager() == 0 && user.getDistributor() == 0) {
//			List<VideoCourse> videoCourses = videoCourseService.selectFreeVideo();//查询普通用户免费的视频。
//			return Msg.success().add("videoCourses", videoCourses);//将普通用户免费的返回
//		}else {
//			List<VideoCourse> videoCourses = videoCourseService.selectVipFreeVideo();
//			return Msg.success().add("videoCourses", videoCourses);//将非普通用户免费的返回
//		}
		return null;
	}
	

	/**
	 *	查询最新视频。
	 * @return
	 */
	@RequestMapping("/newVideo")
	@ResponseBody
	public Msg newVideo() {
		//查询最新的几条视频数据。
//		List<VideoCourse> videoCourses = videoCourseService.selectNewVideo();
//		return Msg.success().add("videoCourses", videoCourses);
		return null;
	}
	
	/**
	 *	最热门视频
	 * @return
	 */
	@RequestMapping("/hottestVideo")
	@ResponseBody
	public Msg hottestVideo() {
		
//		List<VideoCourse> videoCourses = videoCourseService.selectHottestVideo();
//
//		return Msg.success().add("videoCourses", videoCourses);
		return null;
	}
	
	/**
	 *	查询全部视频
	 * @return
	 */
	@RequestMapping("/allVideo")
	@ResponseBody
	public Msg allVideo() {
		//查询所有的视频
//		List<VideoCourse> videoCourses = videoCourseService.selectAllVideo();
//		return Msg.success().add("videoCourses", videoCourses);
		return null;
	}
	
	/**
	 *	视频详情
	 * @param userId			用户id
	 * @param videoCourseId		视频id
	 * @return
	 */
	@RequestMapping("/videoDetails")
	@ResponseBody
	public Msg videoDetails(Integer userId, Integer videoCourseId) {
		
		VideoCourse videoCourse = videoCourseService.selectByPrimaryKey(videoCourseId);//查询视频详情
		int teacherId = videoCourse.getTeacherId();//获取老师的id
		Teacher teacher = teacherService.selectByPrimaryKey(teacherId);//通过老师id查询老师
		Collect collect = collectService.selectCollectByVideoId(userId, videoCourseId,1);//查询当前视频是否已经收藏
		int count = 1;//默认当前视频已经是收藏状态
		if(collect == null) {
			count = -1;//设置视频状态为没有收藏
		}
		
		return Msg.success().add("videoCourse", videoCourse).add("teacher", teacher).add("count", count);
	}
	
	
	
	
	
	
	
}































